//
//  ToastModifier.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/1/15.
//

import SwiftUI
import Kingfisher
import Combine
import LeanCloud


//给View新增扩展方法
extension View {
    public func showVoiceWait(isPresent:Binding<Bool>, bean: VoiceCallParam) -> some View {
        //modifier 给当前试图进行修改
        modifier(showVoiceWaitDialogModifier(isPresent: isPresent, bean: bean))
    }
}

/**
 * 试图修改器
 * VoiceWaitDialog
 */
struct showVoiceWaitDialogModifier: ViewModifier {
    @Binding var isPresent: Bool
    var bean: VoiceCallParam
    
    func body(content: Content) -> some View {
        ZStack {
            content
            if isPresent {
                ZStack {
                    VoiceWaitDialog(bean: bean)
                }.frame(maxHeight: .infinity,alignment: .top)
                    .transition(.move(edge: .top))
                    .animation(.easeInOut(duration: 2), value: isPresent)
            }
        }
    }
    
}

//语音待接
struct VoiceWaitDialog : View {
    var bean: VoiceCallParam
    
    var body: some View {
        HStack(spacing: 0) {
            KFImage(URL(string: bean.logo.formatCdn()))
                .resizable()
                .placeholder{
                    Image(.appDefaultLogo)
                        .resizable()
                        .scaledToFill()
                        .padding(EdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10))
                        .frame(width: 50,height: 50)
                        .clipShape(Circle())
                }
                .background(Color.hex("515151").opacity(0.1))
                .scaledToFill()
                .frame(width: 50,height: 50)
                .clipShape(Circle())
            VStack(alignment: .leading){
                Text(bean.showName ?? "")
                    .font(.system(size: 18))
                    .fontWeight(.bold)
                    .foregroundStyle(.white)
                Text("语音通话")
                    .font(.system(size: 14))
                    .fontWeight(.bold)
                    .foregroundStyle(Color.hex("999999"))
            }.padding(.leading,15)
            Spacer()
            Image(.homeVoiceIconCallReject)
                .resizable()
                .frame(width: 40,height: 40)
                .contentShape(Rectangle())
                .onTapGesture {
                    AudioPlayerManager.shared.pause()
                    commonManager.hideVoiceWait()
                }
            Image(.homeVoiceIconCallAccept)
                .resizable()
                .frame(width: 40,height: 40)
                .padding(.leading,15)
                .contentShape(Rectangle())
                .onTapGesture {
                    //跳转到具体语音界面
                    bean.isStart = true//接听
                    commonManager.showVoiceCall(bean)
                }
        }.frame(maxWidth: .infinity)
            .padding(.horizontal,15)
            .padding(.vertical,30)
            .background(.black.opacity(0.8))
            .cornerRadius(15)
            .padding(.horizontal,15)
            .contentShape(Rectangle())
            .onAppear{
                if bean.startBgVoice {
                    //播放来电声音
                    AudioPlayerManager.shared.playLocal(forResource:"voice",ofType:"mp3", looper : true)
                }
            }
            .onTapGesture {
                //跳转到具体语音界面
                bean.startBgVoice = false //因为这个界面已经在播放了，跳转到那个界面就没必要在播放了
                commonManager.showVoiceCall(bean)
            }
            .onDisappear {
                //销毁不暂停，因为有跳转到语音界面需要继续播放
//                AudioPlayerManager.shared.pause()//暂停播放
            }
    }
}

//#Preview {
//    VoiceCallView()
//}
//定义预览提供者
struct VoiceWaitDialog_Previews: PreviewProvider {
    
    static var previews: some View {
        //使用预览共享配置
        configurePreviewEnvironment(for: VoiceWaitDialog(bean: VoiceCallParam(showName: "我是名字")))
    }
}

